package com.ews.onlineexchange.controller.pub;

import java.util.Date;
import java.util.List;
import javax.inject.Named;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.QualificationTemplate;
import com.ews.onlineexchange.model.Sector;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.service.EntityQuery;

@Named
public class ServiceSupplierListForRest extends EntityQuery<ServiceSupplier> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String cnname;
	private Date startDate;
	private Date endDate;
	private String qualificationClassId;
	private String sectorId;
	private String childSectorId;
	
	public ServiceSupplierListForRest() {
		setMaxResults(EntityQuery.MAX_RESULTS_FEW);
	}

	@Override
	public String getOrder() {		
		return "score DESC,id ";
	}

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<ServiceSupplier> entity) {
		List<Predicate> restrictions = super.getRestrictions(cb, entity);				
		Join<Object, Object> cert = null;
		
		if (!StringUtils.isBlank(qualificationClassId)) {
			if(cert == null )cert = entity.join("certs");
			restrictions.add(cb.isTrue(cert.<Boolean>get("valid")));
			restrictions.add(cb.isTrue(cert.<Boolean>get("auditResult")));
			Join<QualificationTemplate, QualificationClass> clazz = cert.join("qualificationClass");
			restrictions.add(cb.equal(clazz.get("id"), qualificationClassId));
		}
		
		Join<QualificationClass, Sector> sector = null;
		if (!StringUtils.isBlank(sectorId) ) {
			if(cert == null )cert = entity.join("certs");
			sector = cert.join("qualificationClass").join("rootSectors");	
			restrictions.add(cb.equal(sector.get("id"), sectorId));
		}
		if(!StringUtils.isBlank(childSectorId)) {
			if(cert == null )cert = entity.join("certs");
			sector = cert.join("qualificationTemplates").join("sectors");	
			restrictions.add(cb.like(sector.get("sno"), getStringRestriction(childSectorId)));
		}
		if(!StringUtils.isBlank(cnname)) {
			restrictions.add(cb.like(entity.get("cnname"), getStringRestriction(getCnname())));
		}
		if(startDate != null) {
			restrictions.add(cb.greaterThanOrEqualTo(entity.get("createDate"), startDate));
		}
		if(endDate != null) {
			restrictions.add(cb.lessThanOrEqualTo(entity.get("createDate"), endDate));
		}
		restrictions.add(cb.isTrue(entity.<Boolean>get("auditted")));
		restrictions.add(cb.isTrue(entity.<Boolean>get("valid")));
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		String region = (String)getParameterValues().get("region");
		
		return restrictions;
	}

	public String getCnname() {
		return cnname;
	}

	public void setCnname(String cnname) {
		this.cnname = cnname;
	}

	@Override
	protected boolean isDistinct() {
		return true;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getQualificationClassId() {
		return qualificationClassId;
	}

	public void setQualificationClassId(String qualificationClassId) {
		this.qualificationClassId = qualificationClassId;
	}

	public String getSectorId() {
		return sectorId;
	}

	public void setSectorId(String sectorId) {
		this.sectorId = sectorId;
	}

	public String getChildSectorId() {
		return childSectorId;
	}

	public void setChildSectorId(String childSectorId) {
		this.childSectorId = childSectorId;
	}
	
	
	
}
